package com.xl.rentkeeper.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.xl.rentkeeper.R;

import java.util.ArrayList;

/**
 * Created by Administrator on 2015/8/4.
 */
public class SelectPopWindow extends PopupWindow implements View.OnClickListener {
    private View conentView;
    ArrayList<String> listQuesType;//提交问题
    private ListView mLv;
    float scale;
    private int mSelectedItem = 0;//设置问题类型的默认选中按钮
    private String TAG = "SelectePopWindow";


    public SelectPopWindow(final Context context) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        conentView = inflater.inflate(R.layout.selecte_popwindow, null);
        scale = context.getResources().getDisplayMetrics().density;
        // int h = context.getWindowManager().getDefaultDisplay().getHeight();
        //int w = context.getWindowManager().getDefaultDisplay().getWidth();

        //ImageView close = (ImageView) conentView.findViewById(R.id.sele_pop_iv_close);
        Log.d("time","密度 "+scale);

        mLv = (ListView) conentView.findViewById(R.id.sele_pop_lv);
        TextView submit = (TextView) conentView.findViewById(R.id.select_pop_submit);
        TextView close = (TextView) conentView.findViewById(R.id.select_pop_cancel);

        close.setOnClickListener(this);
        submit.setOnClickListener(this);

        initData();



        /*WindowManager.LayoutParams params= WindowManager.LayoutParams;
        params.alpha=0.7f

        getActivity().getWindow().setAttributes(params);*/

        // 设置SelectPicPopupWindow的View
        SelectPopWindow.this.setContentView(conentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        SelectPopWindow.this.setWidth((int) (630*4*scale /9 ));
//        SelectPopWindow.this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        SelectPopWindow.this.setHeight((int) (658*4*scale /9));
//        SelectPopWindow.this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        SelectPopWindow.this.setFocusable(true);
        SelectPopWindow.this.setOutsideTouchable(true);
        // 刷新状态
        SelectPopWindow.this.update();
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(Color.WHITE);
        // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener ，设置其他控件变化等操作
        SelectPopWindow.this.setBackgroundDrawable(dw);



        //给问题选择listview设置适配器
        MyAdapter myAdapter = new MyAdapter(LayoutInflater.from(context));
        mLv.setAdapter(myAdapter);

    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.select_pop_cancel:
                dismiss();
                break;

            case R.id.select_pop_submit:
                dismiss();
                break;
        }
    }


    private void initData() {

        listQuesType = new ArrayList<String>();
            listQuesType.add("电器问题");
            listQuesType.add("费用问题");
            listQuesType.add("堵塞问题");
            listQuesType.add("保洁问题");
            listQuesType.add("门锁问题");
            listQuesType.add("合约问题");
            listQuesType.add("网络问题");
            listQuesType.add("奖励问题");
            listQuesType.add("标配缺失问题");
            listQuesType.add("家具问题");
            listQuesType.add("其他问题");

    }


    class MyAdapter extends BaseAdapter {
        public MyAdapter(LayoutInflater inflater) {

            this.inflater = inflater;
        }

        private LayoutInflater inflater;

        @Override
        public int getCount() {
            return listQuesType.size();
        }

        @Override
        public Object getItem(int position) {
            return listQuesType.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final QuesHolder holder;
            QuesHolder holder1 = null;
            //TextView textView;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.selecte_pop_item, null, false);
                holder = new QuesHolder();
                holder.quesType = (TextView) convertView.findViewById(R.id.select_pop_item_tv);
                holder.mIMIsSelect = (ImageView) convertView.findViewById(R.id.select_pop_item_iv);
                convertView.setTag(holder);
            } else {
                holder = (QuesHolder) convertView.getTag();
            }
            holder.quesType.setText(listQuesType.get(position));

            if (position == mSelectedItem) {
                holder.mIMIsSelect.setSelected(true);
                holder.mIMIsSelect.setClickable(true);
                //convertView.setClickable(true);
                //convertView.setC
                // holder1 = holder;
                Log.d(TAG, "选中了" + mSelectedItem + convertView.isSelected());
            } else {
                holder.mIMIsSelect.setSelected(false);

            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelectedItem = position;
                    notifyDataSetChanged();
                    Log.d(TAG, "点击了" + mSelectedItem);

                }
            });


            return convertView;
        }
    }

    //提交问题的holder
    class QuesHolder {
        TextView quesType;
        ImageView mIMIsSelect;
    }

}
